package com.hongning.railway.ui.main.mine.score;

import android.app.Application;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.hongning.railway.app.Constant;
import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqScoreBean;
import com.hongning.railway.entity.resbean.ResScoreRankInfo;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class MainScoreFragmentViewModel extends BaseViewModel<AppRepository> {
    private ResScoreRankInfo mResScoreRankInfo;

    public void setmResScoreRankInfo(ResScoreRankInfo mResScoreRankInfo) {
        this.mResScoreRankInfo = mResScoreRankInfo;
    }

    public BindingCommand rankzhibuClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.EXTRA_STRING,"支部排行");
            bundle.putParcelable(Constant.EXTRA_DATA,mResScoreRankInfo);
            startContainerActivity(RankFragment.class.getCanonicalName(),bundle);
        }
    });
    public BindingCommand rankallClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.EXTRA_STRING,"总排行");
            bundle.putParcelable(Constant.EXTRA_DATA,mResScoreRankInfo);
            startContainerActivity(RankFragment.class.getCanonicalName(),bundle);
        }
    });
    public BindingCommand scoreallClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startContainerActivity(SocreDetailFragment.class.getCanonicalName());
        }
    });

    public ObservableField<String> score = new ObservableField<>();
    public ObservableField<String> hnscore = new ObservableField<>();
    public ObservableField<String> socore_sub = new ObservableField<>();
    public ObservableField<String> rankzhibu = new ObservableField<>("第2名");
    public ObservableField<String> rankall = new ObservableField<>("第20名");

    public MainScoreFragmentViewModel(@NonNull Application application) {
        super(application);
    }

    public MainScoreFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public void initViewData(){
        ReqScoreBean reqScoreBean = new ReqScoreBean();
        reqScoreBean.setStaffId(Apputils.getStaffid());
        model.getScoreRank(reqScoreBean)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<ResScoreRankInfo>() {
                    @Override
                    public void onResult(ResScoreRankInfo resScoreRankInfo) {
//                        mResScoreRankInfo=resScoreRankInfo;
                        rankall.set("第"+resScoreRankInfo.getTotleRank()+"名");
                        rankzhibu.set("第"+resScoreRankInfo.getGroupRank()+"名");
                        if (mResScoreRankInfo!=null){
                            mResScoreRankInfo.setTotleRank(resScoreRankInfo.getTotleRank());
                            mResScoreRankInfo.setGroupRank(resScoreRankInfo.getGroupRank());
                        }
//                        score.set(String.valueOf(resScoreRankInfo.getSumScore()));
//                        socore_sub.set(String.valueOf(resScoreRankInfo.getStrongCountryScore()));
                    }
                });
    }

}
